package cn.edu.swpu.cins.u_chain.frontend.service.user.Impl;

import cn.edu.swpu.cins.u_chain.Exception.user.OtherException;
import cn.edu.swpu.cins.u_chain.Exception.user.UserException;
import cn.edu.swpu.cins.u_chain.frontend.dao.user.OtherDao;
import cn.edu.swpu.cins.u_chain.frontend.dao.user.UserDao;
import cn.edu.swpu.cins.u_chain.frontend.entity.dto.user.UpdateRole;
import cn.edu.swpu.cins.u_chain.frontend.entity.dto.user.other.OtherDetail;
import cn.edu.swpu.cins.u_chain.frontend.entity.dto.user.other.OtherInfo;
import cn.edu.swpu.cins.u_chain.frontend.entity.persistence.user.LocationInfo;
import cn.edu.swpu.cins.u_chain.frontend.entity.persistence.user.other.OtherInfoDB;
import cn.edu.swpu.cins.u_chain.frontend.entity.persistence.user.other.ProfessionalInfo;
import cn.edu.swpu.cins.u_chain.frontend.enums.UserRole;
import cn.edu.swpu.cins.u_chain.frontend.service.user.OtherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;

@Service
public class OtherServiceImpl implements OtherService {

    @Autowired
    private OtherDao otherDao;
    @Autowired
    private UserDao userDao;
    @Override
    @Transactional(rollbackFor = {RuntimeException.class,OtherException.class,SQLException.class})
    public int addInfo(OtherInfo otherInfo) {
        ProfessionalInfo professionalInfo = new ProfessionalInfo(otherInfo);
        otherDao.addProfessional(professionalInfo);
        int professionalId = professionalInfo.getProfessionalId();
        OtherInfoDB otherInfoDB = new OtherInfoDB(otherInfo);
        otherInfoDB.setProfessionalId(professionalId);
        otherDao.addOtherInfo(otherInfoDB);
        userDao.updateRole(new UpdateRole(otherInfo.getUserId(), UserRole.OTHER.getRole()));
        return 1;
    }

    @Override
    public OtherDetail selectOtherDetail(int userId) {
        return otherDao.selectOtherDetail(userId);
    }
}
